/**
 * Copyright 2010-2012 initialize4j.org
 * 
 * <pre>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * </pre>
 */
package org.initialize4j.service;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.initialize4j.Initialize;
import org.initialize4j.service.filter.InitializeFilter;
import org.initialize4j.service.modify.CommandBuilder;

import com.google.common.collect.Collections2;

/**
 * Implementation of the {@link InitializeService} interface.
 * 
 * @author <a href="hillger.t@gmail.com">hillger.t</a>
 */
class InitializeServiceImpl implements InitializeService {

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final <T> T create(Class<T> clazz, String... scopes) throws InitializeException {
		try {
			T instance = clazz.newInstance();
			initialize(instance, scopes);
			return instance;
		}
		catch (Exception e) {
			throw new InitializeException(e);
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public final void initialize(Object bean, String... scopes) throws InitializeException {
		Collection<Field> annotatedFields = Collections2.filter(Arrays.asList(bean.getClass().getDeclaredFields()), new InitializeFilter());
		try {
			for (Field field : annotatedFields) {
				initializeBeanFieldWithScopes(bean, field, scopes);
			}
		}
		catch (Exception e) {
			throw new InitializeException(e);
		}
	}

	/**
	 * Handles the field initialization of a given object.
	 * 
	 * @param bean The object in the scope of initialization.
	 * @param field The field which maybe gets initialized.
	 * @param scopes An optional list of scopes to allow.
	 */
	private void initializeBeanFieldWithScopes(Object bean, Field field, String... scopes) throws Exception {
		Initialize initialize = field.getAnnotation(Initialize.class);

		if (scopes.length > 0 && !new InitializeScopeInspector().isScopeSatisfied(initialize, scopes)) {
			return;
		}
		
		Object value = FieldUtils.readField(field, bean, true);
		if (!new InitializePreconditionInspector().isPreconditionSatified(value, field)) {
			return;
		}
		CommandBuilder.of(initialize).useBean(bean).useField(field).build().execute();
	}
}
